import {createStore} from "vuex";
import {roomJson} from "@/js/util/room";
import {parser} from "xijs";

export const store = createStore({
    state() {
        return {
            role: "KP",
            cardList: [],
            soulMagicMarkType: {
                'magicStart': {
                    name: '法阵汇成',
                    complexity: 0,
                    mp: '0',
                    changeAble: {},
                    as: (mark) => {
                        return ['法阵']
                    },
                    need: '法术',
                    check: (startMark) => {
                    },
                    effect: '所有魂魔法的启动，即魂魔法的三要素聚集的瞬间，用魔力改造世界吧',
                    rank: 1
                }, 'empty': {
                    name: '未写入',
                    complexity: 0,
                    mp: '0',
                    changeAble: {},
                    as: (mark) => {
                        return []
                    },
                    need: '任何',
                    check: (startMark) => {
                    },
                    effect: '额，就，没写刻印类型的意思，记得换成你要的类型',
                    rank: 1
                },
                'multiMagic': {
                    name: '多重法术',
                    complexity: 1,
                    mp: (mp) => {
                        return mp
                    },
                    changeAble: {
                        '个数': '2'
                    },
                    as: (mark) => {
                        let num = parseInt(mark.changeAble['个数'])
                        return new Array(isNaN(num) ? 0 : num).fill('法阵')
                    },
                    need: '法阵',
                    check: (startMark) => {
                    },
                    effect: '复杂法术的起点，听起来很深奥的样子，实质上就是把多个法阵画在一个里来减少同时吟唱的麻烦罢了',
                    rank: 2
                },
                'overCharge': {
                    name: '末端过充',
                    complexity: 3,
                    mp: '3(可额外注入法力强化)',
                    changeAble: {},
                    as: (mark) => {
                        return ['法阵']
                    },
                    need: '法阵',
                    check: (startMark) => {
                    },
                    effect: '傻子才玩机关枪，你不能指望针能扎死巨兽，真正的解决方案是大，大就是美！火力即一切！允许你注入过量法力来强化这条刻印链末尾的刻印，每过冲一倍魔力，末位刻印强化1.25倍',
                    rank: 3
                },
                'circleMagic': {
                    name: '脉冲释放',
                    complexity: 3,
                    mp: (mp) => {
                        return 0 + '(每次释放消耗' + mp + ')'
                    },
                    changeAble: {},
                    as: (mark) => {
                        return ['法阵']
                    },
                    need: '法阵',
                    check: (startMark) => {
                    },
                    effect: '修改魔力的注入逻辑来制作不断释放法术的刻印，我承认，任何法术都能解决任何问题，解决不了就是不够多，本质上是数量的问题',
                    rank: 3
                }, 'moveMagic': {
                    name: '法阵发射',
                    complexity: 1,
                    mp: '1',
                    changeAble: {
                        '发射方向': '正前方'
                    },
                    as: (mark) => {
                        return ['法阵']
                    },
                    need: '法阵',
                    check: (startMark) => {
                    },
                    effect: '将法阵本身发射出去来避免现象一但离开了法阵就难以操作的问题，但也意味着无法靠直接操作法阵来调整刻印的状态，如果刻印中有雕刻在实体上的刻印更是会直接破坏法阵',
                    rank: 1
                }, 'remoteMagic': {
                    name: '远程施法',
                    complexity: 5,
                    mp: '5',
                    changeAble: {
                        '施法位置': '最近生物'
                    },
                    as: (mark) => {
                        return ['法阵']
                    },
                    need: '法阵',
                    check: (startMark) => {
                    },
                    effect: '法阵发射的替代方案，将法阵的概念投射到远方，令现象也在远方产生，就好像法阵就在那里一样，代价是更高的魔力消耗和复杂度',
                    rank: 3
                }, 'subMagic': {
                    name: '子魔法',
                    complexity: 5,
                    mp: '5',
                    changeAble: {
                        '释放的魔法': '自身'
                    },
                    as: (mark) => {
                        return ['现象']
                    },
                    need: '法阵',
                    check: (startMark) => {
                    },
                    effect: '稍微有些复杂，但是能在需要的时候造出法阵，释放一个新魔法，新魔法的魔力还要正常消耗就是了',
                    rank: 3
                }, 'lockOn': {
                    name: '对象指定',
                    complexity: 1,
                    mp: '0',
                    changeAble: {
                        '指定的对象': '正前方最近物体'
                    },
                    as: (mark) => {
                        return ['物体']
                    },
                    need: '法阵',
                    check: (startMark) => {
                    },
                    effect: '将某个目标指定为法术影响的对象的刻印，只能影响和法阵很近（10厘米）的目标',
                    rank: 1
                }, 'cast': {
                    name: '抛射',
                    complexity: 2,
                    mp: '3',
                    changeAble: {
                        '方向': '正前方'
                    },
                    as: (mark) => {
                        return ['现象']
                    },
                    need: '物体',
                    check: (startMark) => {
                    },
                    effect: '虽说叫抛射，但也不一定有抛物线，实质上是将法阵中的较轻触媒（小于10kg）发射出去的技术。',
                    rank: 1
                }

            },
            stateGroup: {
                HP: ['健康', '重伤', '昏厥', '伤昏', '死亡'],
                MP: ['充盈', '枯竭', '乱流', '魂灭']
            },
            stateGroupColor: {
                HP: {
                    '健康': '#67C23A',
                    '重伤': '#e80000',
                    '昏厥': '#605f5f',
                    '伤昏': '#914141',
                    '死亡': '#6c0000'
                },
                MP: {
                    '充盈': '#6f18c7',
                    '枯竭': '#5b2e00',
                    '乱流': '#c700ab',
                    '魂灭': '#000000'
                }
            },
            cardIndex: 1,
            link: undefined,
            message: [],
            name: ''
        }
    },
    getters: {
        visibleCardList(state) {
            if (state.role === 'KP') {
                return state.cardList
            } else {
                return state.cardList.filter((ele) => {
                    return ele.PLVisible
                })
            }
        },
        cardData: (state) => (index, keyStack) => {

            let card = state.cardList.find((ele) => ele.id+'' === index+'')
            if (card == null) {
                return ''
            }
            let outData = card.data
            for (let Ikey of keyStack) {
                outData = outData[Ikey]
            }
            return outData
        },
        cardByIndex: (state) => (id) => {
            if (id+''==='-1'){
                return state.cardList[0]
            }
            return state.cardList.find((card) => {
                return (card.id + '') === (id + '')
            })
        },
        isKP(state) {
            if (state.role === 'KP') {
                return true
            }
        }
    },
    mutations: {
        pushCardList(state, card) {
            if (card.id===-1){
                while (store.getters.cardByIndex(state.cardIndex) != null) {
                    state.cardIndex++
                }
                card.id = state.cardIndex++
            }else if (store.getters.cardByIndex(card.id) != null){
                store.commit("deleteCardList",card.id)
            }
            state.cardList.push(card)
            if (store.getters.isKP){
                sendMessage(state.link,
                    parser.stringify({
                        type:'card_add',
                        data:{card:card}
                    })
                )
            }
        },
        cardData(state, arg) {
            let data = arg.data
            let index = arg.index
            let keyStack = [...arg.keyStack]
            let card = state.cardList.find((ele) => ele.id === index)
            if (card == null) {
                return
            }
            keyStackSet(card.data, data, keyStack)
            if (store.getters.isKP&&card.PLVisible){
                sendMessage(state.link,
                    parser.stringify({
                        type:'card_update',
                        data:{index:index,card:card}
                    })
                )
            }
        },
        updateCard(state,arg){
            let card = arg.card
            let index = arg.index
            let lCard = state.cardList.find((ele) => {
               return  ele.id === index
            })
            state.cardList.splice(state.cardList.indexOf(lCard),1,card)
        },
        deleteCardList(state, index) {
            let card = state.cardList.find((card) => {
                return (card.id + '') === (index + '')
            })
            if (store.getters.isKP){
                sendMessage(state.link,
                    parser.stringify({
                        type:'card_delete',
                        data:{index:index}
                    })
                )
            }
            state.cardList.splice(state.cardList.indexOf(card), 1)
        },
        toggleCardHidden(state, index) {
            let card = state.cardList.find((card) => {
                return (card.id + '') === (index + '')
            })
            card.PLVisible = !card.PLVisible
            sendMessage(state.link,
                    parser.stringify({
                        type:'card_update',
                        data:{index:index,card:card}
                    }))


        },
        switchRole(state, role) {
            state.role = role
        },
        initRoom(state, room) {
            if (room.cardList != null) state.cardList = room.cardList.filter((ele)=>{return ele!=null})
            if ((room.localRead ?? false) && store.getters.isKP) {
                sendMessage(state.link,
                    parser.stringify({
                        type:'init_room',
                        data:roomJson(),
                        init_user:'$*'
                    })
                )
            }
        },
        getLink(state, {link: link, name: name}) {
            state.link = link
            state.name = name
        },
        outLink(state) {
            if (state.link != null) {
                closeLink(state.link)
                state.link = undefined
            }
        },
        pushMessage(state, message) {
            state.message.push(message)
        },
        emptyMessage(state) {
            state.message = []
        },
        send(state,text){
            sendMessage(state.link,text)
        }
    }
})

function keyStackSet(target, data, keyStack) {
    if (keyStack.length === 1) {
        if (typeof data != 'function') {
            target[keyStack[0]] = data
        } else {
            data(target[keyStack[0]])
        }
    } else if (keyStack.length > 1) {
        keyStackSet(target[keyStack[0]], data, keyStack.splice(1, keyStack.length - 1))
    } else {
        if (typeof data != 'function') {
            Object.assign(target, data)
        } else {
            data(target)
        }
    }
}

function sendMessage(link, message) {
    if (link != null)
        link.send(message)
}

function closeLink(link) {
    link.close()
}
